package com.wx.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.wx.entity.WxTextReply;
import com.wx.entity.WxTextReplyExample;
import com.wx.service.WxTextReplyService;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.bean.PropertiesUtilBean.PropertiesUtils;
import com.zyht.common.util.Page;
import com.zyht.website.entity.SysUser;

@Controller
@RequestMapping("admin/txt")
public class TxtMsgConfigController extends AbsManageBaseController {
	@Resource
	private WxTextReplyService wxTextReplyService;

	@Value("#{propertiesReader[session_user_key]}")
	private String sessionKey;
	@Value("#{propertiesReader[session_wx_key]}")
	private String session_wx_key;

	@RequestMapping("toAdd")
	public String toAdd(Model model) {
		return "basic/text/add";
	}

	@RequestMapping("add")
	public String addTextKeywords(@ModelAttribute WxTextReply entity, Model model, HttpServletRequest request) {
		String wxid = getWxId(request);
		entity.setWxid(wxid);
		entity.setCreateTime(new Date());
		SysUser sysUser = getSessionUser(request);
		if (entity.getId() == null) {
			entity.setUid(sysUser.getId());
		}
		this.wxTextReplyService.insert(entity);
		model.addAttribute("redirect", "admin/txt/list");
		return "message";
	}

	@RequestMapping("toEdit")
	public ModelAndView toEdit(Integer id, HttpServletRequest request) {
		WxTextReply entity = this.wxTextReplyService.selectByPrimaryKey(id);
		String wxid = getWxId(request);
		ModelAndView view = new ModelAndView();
		view.setViewName("basic/text/edit");
		view.addObject("wxid", wxid);
		view.addObject("bean", entity);
		return view;
	}

	@RequestMapping("edit")
	public String editTextKeywordsById(@ModelAttribute WxTextReply entity, Model model) {
		WxTextReply bean = this.wxTextReplyService.selectByPrimaryKey(entity.getId());
		bean.setKeyword(entity.getKeyword());
		bean.setKeywordType(entity.getKeywordType());
		bean.setContent(entity.getContent());
		this.wxTextReplyService.updateByPrimaryKey(bean);
		model.addAttribute("redirect", "admin/txt/list");
		return "message";
	}

	@RequestMapping("list")
	public ModelAndView list(Integer currentPage, HttpServletRequest request) {
		ModelAndView view = new ModelAndView("basic/text/list");
		String wxid = getWxId(request);

		WxTextReplyExample example = new WxTextReplyExample();
		example.createCriteria().andWxidEqualTo(wxid);
		example.setOrderByClause("create_time desc");

		int total = this.wxTextReplyService.countByExample(example);
		if (null == currentPage) {
			currentPage = 1;
		}
		String pageSizeStr = PropertiesUtils.getConfig("paganation_page_size");
		int pageSize = Integer.parseInt(pageSizeStr);
		PageHelper.startPage(currentPage, pageSize);
		List<WxTextReply> list = this.wxTextReplyService.selectByExample(example);

		Page<WxTextReply> page = new Page<WxTextReply>(total, currentPage, pageSize);
		page.setRecordList(list);

		view.addObject("page", page);
		return view;
	}

	@RequestMapping("del")
	public String removeById(Integer id, Model model) {
		model.addAttribute("msg", "删除成功");
		if (null == id) {
			model.addAttribute("msg", "非法参数");
		} else {
			this.wxTextReplyService.deleteByPrimaryKey(id);
		}
		model.addAttribute("redirect", "admin/txt/list");
		return "message";
	}
}
